/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-01-13
 * Time: 9:53
 */
public class Test {
    public static void main(String[] args) {
        System.out.println("hello");
    }
    //直接插入排序
    public static void insertSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;

            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];


                } else {
                    array[j + 1] = tmp;
                    break;
                }
            }
            if (j == -1) {
                array[j + 1] = tmp;
            }
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            shell(array, gap);
            gap /= 2;
        }
//整体进行插入排序
        shell(array, 1);
    }
    //希尔排序

    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;

            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];


                } else {
                    array[gap + 1] = tmp;
                    break;
                }
            }
            if (j == -1) {
                array[j + gap] = tmp;
            }
        }


    }

    //选择排序
    public static void selectSort(int[] array) {
        int minIndex = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);

        }

    }

    private static void swap(int[] array, int i, int minIndex) {
        int tmp = array[i];
        array[i] = array[minIndex];
        array[minIndex] = tmp;
    }

    //
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            shiftDown(array, 0, end);
            end--;
        }
    }

    private static void createBigHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(array, parent, array.length);
        }
        private static void shiftDown ( int[] array, int parent, int len){
            int child = 2 * parent + 1;
            while (child < len) {
                if (child + 1 < len && array[child] < array[child + 1]) {
                    child++;
                }
                if (array[child] > array[parent]) {
                    swap(array, child, parent);
                    parent = child;
                    child = 2 * parent + 1;
                } else {
                    break;
                }
            }
        }


    }


    private static void shiftDown(int[] array,int parent,int len) {
        int child = 2*parent+1;
        while (child < len) {
            if(child+1 < len && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }

}
